package com.yjc.rabbitMQ.eight;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.yjc.rabbitMQ.utils.RabbitMQUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shkstart
 * @create 2022-08-02 11:55
 */
public class Consumer01 {

    //普通交换机
    private static final String NORMAL_EXCHANGE = "normal_exchange";
    //普通队列
    private static final String NORMAL_QUEUE = "normal_queue";
    //死信交换机
    private static final String DEAD_EXCHANGE = "dead_exchange";
    //死信队列
    private static final String DEAD_QUEUE = "dead_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        Map<String, Object> arguments = new HashMap<>();

        //正常队列设置消息过期时间（建议在生产方设置）
//        arguments.put("x-message-ttl", 10000);
        //正常队列设置死信交换机
        arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        //正常队列设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key", "lisi");
        //设置正常队列的长度限制
//        arguments.put("x-max-length", 6);

        //声明正常队列
        channel.queueDeclare(NORMAL_QUEUE, false, false, false, arguments);
        channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangsan");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String msg = new String(message.getBody(), "UTF-8");
            if ("info 5".equals(msg)) {
                System.out.println("Consumer01(正常队列)拒绝接收消息是：" + msg);
                channel.basicReject(message.getEnvelope().getDeliveryTag(), false);
            } else {
                System.out.println("Consumer01(正常队列)接收消息是：" + msg);
                channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
            }
        };

        //手动应答
        channel.basicConsume(NORMAL_QUEUE, false, deliverCallback, (consumerTag) -> {
        });

    }
}
